/**
 * Clase para crear el abm de una entidad
 * 
 * @author Miguel E. Diganchi
 */	
qx.Class.define("mutual.abm", {
	extend : qx.ui.groupbox.GroupBox,
	construct : function(dataModel, fnGetData, dataCriteria) {
		if (fnGetData){
			var dataSet = fnGetData(null);
			this.getData = fnGetData;
		}
		this.base(arguments);
		this.setLayout(new qx.ui.layout.VBox(5));

		this.grpFinder = this.loadFindCriteria(dataCriteria);

		// definimos el grupo que contiene a los botones
		this.grpButtons = new qx.ui.groupbox.GroupBox();
		this.grpButtons.setLayout(new qx.ui.layout.HBox(3).set({alignX : "center"}));
		this.btnInsert = new qx.ui.form.Button("Nuevo");
		this.btnInsert.addListener("execute", function() {
					this.initForm(true); // el true indica que es un nuevo
											// registro
				}, this);
		this.btnEdit = new qx.ui.form.Button("Editar");
		this.btnEdit.addListener("execute", function() {
					this.initForm(false); // el true indica que se trata de un
											// edit
				}, this);
		this.btnDelete = new qx.ui.form.Button("Eliminar");
		this.btnDelete.addListener("execute", function() {
					this.deleteAction();
				}, this);
		this.grpButtons.add(this.btnInsert);
		this.grpButtons.add(this.btnDelete);
		this.grpButtons.add(this.btnEdit);

		// definimos el modelo
		this.tblModel = new qx.ui.table.model.Simple();
		this.tblModel.setColumns(dataModel.fields, dataModel.titles);

		// definimos la tabla
		this.tblGrid = new qx.ui.table.Table(this.tblModel);
		if (fnGetData){
			this.loadGrid();
		}
		if (this.grpFinder) {
			this.add(this.grpFinder);
		}
		this.add(this.tblGrid);
		this.add(this.grpButtons);

	},

	members : {
		tblModel : null,
		tblGrid : null,

		grpFinder : null,
		txtFinder : null,
		cboCriteria : null,
		hasFinder : true,

		grpButtons : null,
		btnInsert : null,
		btnEdit : null,
		btnDelete : null,

		getData : null,

		initInsertAction : null,
		deleteAction : null,
		initEditAction : null,

		loadFindCriteria : null,

		initForm : null,
		/* methods */
		hide : function() {
			this.grpFinder.setVisibility("excluded");
			this.grpButtons.setVisibility("excluded");
			this.tblGrid.setVisibility("excluded");
		},
		show : function() {
			this.grpFinder.setVisibility("visible");
			this.grpButtons.setVisibility("visible");
			this.tblGrid.setVisibility("visible");
		},
		loadFindCriteria : function(dataCriteria) {
			if (!dataCriteria) {
				this.hasFinder = false;
				return null; 
			}
			this.grpFinder = new qx.ui.groupbox.GroupBox("Buscador");
			this.grpFinder.setLayout(new qx.ui.layout.HBox(5));
			this.txtFinder = new qx.ui.form.TextField();
			this.txtFinder.addListener("keypress", function(e) {
						if (e.getKeyIdentifier() == "Enter") {
							this.loadGrid();
						}
					}, this);
			this.grpFinder.add(this.txtFinder);
			this.cboCriteria = new qx.ui.form.SelectBox().set({width:200});
			for (var i = 0; i < dataCriteria.names.length; i++) {
				this.cboCriteria.add(new qx.ui.form.ListItem(
						dataCriteria.names[i], "", {
							criteria : dataCriteria.values[i],
							op : dataCriteria.operators[i]
						}));
			}
			this.grpFinder.add(this.cboCriteria);
			return this.grpFinder;
		},
		loadGrid : function() {
			var filter = null;
			if (this.hasFinder) {
				var dataFind = {
					criteria : this.cboCriteria.getModelSelection().getItem(0).criteria,
					operator : this.cboCriteria.getModelSelection().getItem(0).op,
					value : this.txtFinder.getValue()
				}
				filter = this.getCriteria(dataFind);
			}
			var dataSet = this.getData(filter);
			if (!dataSet) {
				if (this.hasFinder && filter){
					alert("no se encontraron resultados con el criterio ingresado");
					dataSet = this.getData(null);
					this.txtFinder.setValue("");
					this.tblGrid.getTableModel().setDataAsMapArray(dataSet);
				} else {
					this.clearDataGrid();
				}
			}else{
				this.tblGrid.getTableModel().setDataAsMapArray(dataSet);
			}
		},
		getSelectedId : function() {
			try{
				var id = this.tblModel.getValue(0, this.tblGrid.getFocusedRow());
				return id;
			}catch(x){
				return null;
			}
		},
		
		setSelectedId : function(id){
	    	for (var i = 0, j = this.tblModel.getRowCount(); i < j; i++) {
				  if (this.tblModel.getValue(0, i) == id) {
				    this.tblGrid.setFocusedCell(null, i);
				    this.tblGrid.getSelectionModel().setSelectionInterval(i, i);
				    break;
				  }
			}
			
		},
		
		getCriteria : function(dataFind) {
			if (!dataFind.value) {
				return null
			}
			switch (dataFind.operator) {
				case 'like' :
					dataFind.value = "'%" + dataFind.value + "%'";
					break;
				case '=' :
					break;
				default :
					return null;
			}
			var filter = dataFind.criteria + "  " + dataFind.operator + " "
					+ dataFind.value;
			return filter;
		},
		
		clearDataGrid : function(){
			this.tblGrid.getTableModel().setData("");
		}
	}
});
